package com.jh.controller;
/**
 * 代号:隐无为 2017：厚溥
 * 文件名：ErrorSubjectController.java
 * 创建人：柯栋
 * 日期：2017年9月21日
 * 修改人：
 * 描述：
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.kd.common.base.controller.BaseController;
import org.kd.utils.JsonReturnData;
import org.kd.utils.PageTable;
import org.kd.utils.WebConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.jh.model.Classify;
import com.jh.model.ErrorSubject;
import com.jh.model.Subject;
import com.jh.service.ClassifyService;
import com.jh.service.ErrorSubjectService;

/**
 * 用途：业务模块名称
 */
@Controller
@RequestMapping("/errorErrorSubjectController")
public class ErrorSubjectController extends BaseController {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	ErrorSubjectService errorErrorSubjectService;

	@RequestMapping("/list")
	public String list(ModelMap modelMap) {
		logger.info("===============" + this.getClass().getName() + "==================");
		return "errorErrorSubject/list";
	}

	@RequestMapping("/saveUpdatePage")
	public String saveUpdatePage(Integer id, ModelMap modelMap) {
		if (id != null) {
			modelMap.put("errorErrorSubject", errorErrorSubjectService.queryById(id));
		}
		return "errorErrorSubject/saveUpdatePage";
	}

	@RequestMapping("/saveUpdate")
	@ResponseBody
	public String saveUpdate(ErrorSubject errorErrorSubject) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "操作失败");
		if (errorErrorSubject.getId() != null) {
			errorErrorSubject.setUpdateDate(new Date());
			errorErrorSubjectService.updateNoNull(errorErrorSubject);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("更新成功");
		} else {
			errorErrorSubject.setCreatDate(new Date());
			errorErrorSubject.setDelFlag(WebConstant.NODELETECODE);
			errorErrorSubjectService.insert(errorErrorSubject);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("添加成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String delete(ErrorSubject errorErrorSubject) {

		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "删除失败");
		if (errorErrorSubject.getId() != null) {
			errorErrorSubject.setDelFlag(WebConstant.DELETECODE);
			errorErrorSubjectService.updateDelete(errorErrorSubject);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/deletes")
	@ResponseBody
	public String deletes(String ids) {
		JsonReturnData<String> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "批量删除失败");
		if (!StringUtils.isEmpty(ids)) {
			// errorErrorSubjectService.updateDeletes(ids);
			jsonReturnData.setCode(WebConstant.SUCCESSCODE);
			jsonReturnData.setMsg("批量删除成功");
		}

		return responseAPI.getJsonString(jsonReturnData);
	}

	@RequestMapping("/ajaxList")
	@ResponseBody
	public String ajaxList(Integer page, Integer limit, ErrorSubject u) {
		PageTable<List<ErrorSubject>> pageTable = new PageTable<List<ErrorSubject>>();
		u.setDelFlag(WebConstant.NODELETECODE);
		PageInfo<ErrorSubject> pageInfo = errorErrorSubjectService.queryPageListByParam(page, limit, u);
		pageTable.setCode(WebConstant.PAGESUCCESSCODE);
		pageTable.setMsg(WebConstant.SUCCESS);
		pageTable.setData(pageInfo.getList());
		pageTable.setCount(pageInfo.getTotal());
		return responseAPI.getJsonString(pageTable);
	}

	/**
	 *  @功能:查询不为null的参数
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年5月6日
	 *  @param page
	 *  @param limit
	 *  @param u
	 *  @return  
	 */
	@RequestMapping("/ajaxListNoNull")
	@ResponseBody
	public String ajaxListNoNull(Integer page, Integer limit, ErrorSubject u) {
		PageTable<List<ErrorSubject>> pageTable = new PageTable<List<ErrorSubject>>();
		u.setDelFlag(WebConstant.NODELETECODE);
		PageInfo<ErrorSubject> pageInfo = errorErrorSubjectService.queryPageListByParamNoNull(page, limit, u);
		pageTable.setCode(WebConstant.SUCCESSCODE);
		pageTable.setMsg(WebConstant.SUCCESS);
		pageTable.setData(pageInfo.getList());
		pageTable.setCount(pageInfo.getTotal());
		return responseAPI.getJsonString(pageTable);
	}
	/**
	 * @功能:获取错误次数
	 * @作者:jh @代号:甲骨兽进化
	 * @时间:2018年5月5日
	 * @param errorErrorSubject
	 * @return
	 */
	@RequestMapping("/queryById")
	@ResponseBody
	public String queryById(ErrorSubject model) {
		JsonReturnData<ErrorSubject> jsonReturnData = new JsonReturnData<>(WebConstant.ERRORCODE, "批量删除失败");
		model.setDelFlag(WebConstant.NODELETECODE);
		// 查询错误次数
		Integer queryCount = errorErrorSubjectService.queryCount(model);
		queryCount++;
		// 向数据库添加错误信息
		model.setCreatDate(new Date());
		errorErrorSubjectService.insert(model);
		// 处理错误次数
		model.setErrorCount(queryCount);
		
		jsonReturnData.setCode(WebConstant.SUCCESSCODE);
		jsonReturnData.setMsg(WebConstant.SUCCESS);
		jsonReturnData.setData(model);
		return responseAPI.getJsonString(jsonReturnData);
	}

}
